import React, { PureComponent } from 'react';
import { Form, Input, Card, Button } from 'antd';
import { connect } from 'dva';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { COLLECT_DETAIL, COLLECT_SUBMIT } from '../../../actions/collect';

const FormItem = Form.Item;

@connect(({ collect, loading }) => ({
  collect,
  submitting: loading.effects['collect/submit'],
}))
@Form.create()
class CollectEdit extends PureComponent {
  componentWillMount() {
    const {
      dispatch,
      match: {
        params: { id },
      },
    } = this.props;
    dispatch(COLLECT_DETAIL(id));
  }

  handleSubmit = e => {
    e.preventDefault();
    const {
      dispatch,
      match: {
        params: { id },
      },
      form,
    } = this.props;
    form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const params = {
          id,
          ...values,
        };
        console.log(params);
        dispatch(COLLECT_SUBMIT(params));
      }
    });
  };

  render() {
    const {
      form: { getFieldDecorator },
      collect: { detail },
      submitting,
    } = this.props;

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
        md: { span: 10 },
      },
    };

    const action = (
      <Button type="primary" onClick={this.handleSubmit} loading={submitting}>
        提交
      </Button>
    );

    return (
      <Panel title="修改" back="/desk/collect" action={action}>
        <Form hideRequiredMark style={{ marginTop: 8 }}>
          <Card className={styles.card} bordered={false}>
            <FormItem {...formItemLayout} label="主键">
              {getFieldDecorator('id', {
                rules: [
                  {
                    required: true,
                    message: '请输入主键',
                  },
                ],
                initialValue: detail.id,
              })(<Input placeholder="请输入主键" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="标题">
              {getFieldDecorator('title', {
                rules: [
                  {
                    required: true,
                    message: '请输入标题',
                  },
                ],
                initialValue: detail.title,
              })(<Input placeholder="请输入标题" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="描述">
              {getFieldDecorator('subTitle', {
                rules: [
                  {
                    required: true,
                    message: '请输入描述',
                  },
                ],
                initialValue: detail.subTitle,
              })(<Input placeholder="请输入描述" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="一级类目">
              {getFieldDecorator('category', {
                rules: [
                  {
                    required: true,
                    message: '请输入一级类目',
                  },
                ],
                initialValue: detail.category,
              })(<Input placeholder="请输入一级类目" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="二级类目">
              {getFieldDecorator('subCategory', {
                rules: [
                  {
                    required: true,
                    message: '请输入二级类目',
                  },
                ],
                initialValue: detail.subCategory,
              })(<Input placeholder="请输入二级类目" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="格式链接、文本">
              {getFieldDecorator('contentType', {
                rules: [
                  {
                    required: true,
                    message: '请输入格式链接、文本',
                  },
                ],
                initialValue: detail.contentType,
              })(<Input placeholder="请输入格式链接、文本" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="内容">
              {getFieldDecorator('content', {
                rules: [
                  {
                    required: true,
                    message: '请输入内容',
                  },
                ],
                initialValue: detail.content,
              })(<Input placeholder="请输入内容" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="备注">
              {getFieldDecorator('remark', {
                rules: [
                  {
                    required: true,
                    message: '请输入备注',
                  },
                ],
                initialValue: detail.remark,
              })(<Input placeholder="请输入备注" />)}
            </FormItem>
            <FormItem {...formItemLayout} label="创建部门">
              {getFieldDecorator('createDept', {
                rules: [
                  {
                    required: true,
                    message: '请输入创建部门',
                  },
                ],
                initialValue: detail.createDept,
              })(<Input placeholder="请输入创建部门" />)}
            </FormItem>
          </Card>
        </Form>
      </Panel>
    );
  }
}

export default CollectEdit;
